﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;
using System.Data;

namespace Runda.EShop.Web.Customer
{
    public partial class ProductListByCateForTerminal : BasePage
    {
        public string CateID
        {
            get
            {
                if (Request.QueryString["CateID"] != null)
                {
                    return Request.QueryString["CateID"];
                }
                return null;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (base.User.Identity.LoginUser.EntType == (int)EnumHelper.EnterpriseType.Agent)
                {
                    Response.RedirectPermanent("ProductListByCateForAgent.aspx");
                    return;
                }

                string Condition = "";
                Condition = " and ProductType=" + ((int)EnumHelper.ProductType.Instrument) + "  and ProductID in(select ProductID from Relation_Customer_Product where Customerid='" + base.User.Identity.LoginUser.UserID.ToString() + "')";
                if (!string.IsNullOrEmpty(CateID))
                {
                    Condition += " and (CateCodeEx LIKE '" + CateID + "%' OR CateCode LIKE '" + CateID + "%')";
                }
                //加载筛选条件(按仪器筛选)
                List<BasProductEntity> list = new BasProductBll().GetAll<BasProductEntity>(Condition, " order by CateCode");
                BasProductEntity item = new BasProductEntity(new Guid("11111111-1111-1111-1111-111111111111"));
                item.ProductName = "零售";
                list.Add(item);
                dlFilter.DataSource = list;
                dlFilter.DataBind();

                Guid? InstrumentID = null;
                if (!string.IsNullOrEmpty(hidSelectInstrument.Value))
                {
                    InstrumentID = new Guid(hidSelectInstrument.Value);
                }
                BasProductBll bll = new BasProductBll();
                DataTable dt = bll.QueryProductTrademark(base.User.Identity.LoginUser.UserID, InstrumentID, CateID, 2);
                dlFilter1.DataSource = dt;
                dlFilter1.DataBind();
                

                BindGrid(ConstHelper.PAGE_INIT_INDEX); 
            }
        }

        private void BindGrid(int PageIndex)
        {
            int RecordCount = 0;
            //MasterDataBll bll = new MasterDataBll();
            //List<BasProductEntity> list = null;
            //if (!string.IsNullOrEmpty(CateID))
            //{
            //    list = bll.GetProductListByCate(CateID,2, "ProductCode ASC", AspNetPager1.PageSize, PageIndex, out RecordCount);
            //}
            //else
            //{
            //    list = bll.GetProductListByCustomer(this.User.Identity.LoginUser.UserID,2, "ProductCode DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);
            //}

            Guid? InstrumentID = null;
            if (!string.IsNullOrEmpty(hidSelectInstrument.Value))
            {
                InstrumentID = new Guid(hidSelectInstrument.Value);
            }
            string Trademark = null;
            if (!string.IsNullOrEmpty(hidSelectTrademark.Value))
            {
                Trademark = hidSelectTrademark.Value.ToString();
            }

            CustomerProductQueryConditionEntity Condition = new CustomerProductQueryConditionEntity();
            Condition.Customer = base.User.Identity.LoginUser;
            Condition.CateType = (int)EnumHelper.CustomerProductCateType.ByFunction;
            Condition.CateCode = CateID;
            Condition.InstrumentID = InstrumentID;
            Condition.Trademark = Trademark;

            BasProductBll bll = new BasProductBll();
            DataTable dt = bll.QueryProduct(Condition, "ProductCode DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);
            gvProductList.DataSource = dt;
            gvProductList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        protected void gvProductList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //BasProductEntity entity = e.Row.DataItem as BasProductEntity;
                //Label stockStatus = e.Row.FindControl("stockStatus") as Label;
                //DataRowView dv = e.Row.DataItem as DataRowView;
                //if (Convert.ToInt32(dv["HasStock"].ToString()) == 0)
                //{
                //    stockStatus.Text = "缺货"; 
                //}
                //else
                //{
                //    stockStatus.Text = "有货"; 
                //}
            }
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        protected void gvProductList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "addto")
            {
                Guid ProductID = new Guid(e.CommandArgument.ToString());
                BizCustomerFavoriteEntity entity = new BizCustomerFavoriteEntity();
                entity.ProductID = ProductID;
                entity.CustomerID = base.User.Identity.LoginUser.UserID;
                entity.CreatedAt = DateTime.Now;
                entity.Deleted = false;

                CustomerBll bll = new CustomerBll();
                bll.AddCustomerFavorite(entity);

                ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('添加成功.');", true);
            }
        }

        protected void dlFilter_ItemCommand(object source, DataListCommandEventArgs e)
        {
            hidSelectInstrument.Value = e.CommandArgument.ToString();

            Guid? InstrumentID = null;
            if (!string.IsNullOrEmpty(hidSelectInstrument.Value))
            {
                InstrumentID = new Guid(hidSelectInstrument.Value);
            }
            BasProductBll bll = new BasProductBll();
            DataTable dt = bll.QueryProductTrademark(base.User.Identity.LoginUser.UserID, InstrumentID, CateID, 2);
            dlFilter1.DataSource = dt;
            dlFilter1.DataBind();

            if (InstrumentID.ToString() == "11111111-1111-1111-1111-111111111111") { lblInstrument.Text = "零售"; }
            else
            {
                lblInstrument.Text = "仪器：" + new BasProductBll().Get<BasProductEntity>(InstrumentID).ProductName;
            }
            BindGrid(ConstHelper.PAGE_INIT_INDEX);
            btnDelete.Visible = true;
        }

        protected void dlFilter1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            hidSelectTrademark.Value = e.CommandArgument.ToString();
            BindGrid(ConstHelper.PAGE_INIT_INDEX);
            btnDelete1.Visible = true;
            lblTrademark.Text = "品牌：" + hidSelectTrademark.Value.ToString();
        }

        
        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            hidSelectInstrument.Value = null;
            BindGrid(ConstHelper.PAGE_INIT_INDEX);
            btnDelete.Visible = false;
            lblInstrument.Text = "仪器";
        }

        protected void btnDelete1_Click(object sender, ImageClickEventArgs e)
        {
            hidSelectTrademark.Value = null;
            BindGrid(ConstHelper.PAGE_INIT_INDEX);
            btnDelete1.Visible = false;
            lblTrademark.Text = "品牌";
        }
    }
}